package com.sfac.springBoot.modules.test.controller;

import com.sfac.springBoot.modules.common.vo.Result;
import com.sfac.springBoot.modules.common.vo.Search;
import com.sfac.springBoot.modules.test.entity.Student;
import com.sfac.springBoot.modules.test.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Description StudentController
 * @Author HymanHu
 * @Date 2022/7/1 15:04
 */
@RestController
@RequestMapping("/api")
public class StudentController {

	@Autowired
	private StudentService studentService;

	/**
	 * 127.0.0.1/api/student ---- post
	 * {"studentName":"HuJiang","age":18}
	 * {"studentName":"HuJiang1","age":19,"card":{"id":1,"cardNo":"0001"}}
	 * {"id":1,"studentName":"HuJiang","age":18}
	 */
	@PostMapping(value = "/student", consumes = "application/json")
	public Result<Student> editStudent(@RequestBody Student student) {
		return studentService.editStudent(student);
	}

	/**
	 * 127.0.0.1/api/student/2 ---- get
	 */
	@GetMapping("/student/{id}")
	public Student getStudentById(@PathVariable int id) {
		return studentService.getStudentById(id);
	}

	/**
	 * 127.0.0.1/api/student?studentName=hujiang ---- get
	 */
	@GetMapping(value="/student")
	public Student getStudentByStudentName(@RequestParam String studentName) {
		return studentService.getStudentByStudentName(studentName);
	}

	/**
	 * 127.0.0.1/api/student ---- put
	 * {"studentName":"hyman","id":1}
	 */
	@PutMapping(value = "/student", consumes = "application/json")
	public Result<Student> updateStudent(@RequestBody Student student) {
		return studentService.updateStudent(student);
	}

	/**
	 * 127.0.0.1/api/students ---- post
	 * {"currentPage":1,"pageSize":5,"sort":"id","direction":"desc","keyword":""}
	 */
	@PostMapping(value = "/students", consumes = "application/json")
	public Page<Student> getStudentsBySearch(@RequestBody Search search) {
		return studentService.getStudentsBySearch(search);
	}
}
